import "reflect-metadata";
import { createConnection } from "typeorm";
import { Captain } from "../entity/Captain";
import { Request, Response } from "express";
import { captains } from "../configs/configs";

const moment = require('moment-timezone');
const connR = createConnection("captain");

export function captainRoute(app: any) {
    
    // app.get('/api/captain/:captain_id', function (req: Request, res: Response) {

    //     connR.then(async connection => {

    //         let data = await connection.getRepository(Captain).findOne(req.params.captain_id);
    //         res.send(data)

    //     }).catch(error => console.log(error));
    // })


    app.get('/captains/:role_id', function (req: Request, res: Response) {

        connR.then(async connection => {

            let where = {role_id: req.params.role_id};
            let data = await connection.getRepository(Captain).find({ where: where, order: { captain_id: 'ASC' } });
            data.forEach(element => {
                element.name = captains[element.captain_id].Name;
            });

            // let data = await connection.getRepository(Captain).createQueryBuilder().select("COUNT(*) AS c").where().getRawOne();
            res.render('captain/index', { data: JSON.stringify(data) })

        }).catch(error => console.log(error));

    })
}
